package com.xiongjie;

import io.reactiverse.pgclient.*;
import io.vertx.core.Vertx;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * 使用普通的方法使用pgclient和使用封装的方式使用pgclient
 */
public class Application {

    public static void main(String[] args) {
        pgClient();
        pgClientVertx();
        pgClientByFeature();
    }

    /**
     * 1.获取PgPool,通过options对象
     * @return
     */
    public static PgPool getPgPoolByOption(){
        PgPoolOptions options = new PgPoolOptions()
                .setPort(5432)
                .setHost("192.168.1.14")
                .setDatabase("xiongjie")
                .setUser("postgres")
                .setPassword("postgres")
                .setMaxSize(5);
        PgPool client = PgClient.pool(options);
        return client;
    }

    /**
     * 2.获取PgPool,通过Url数据库配置路径
     * @return
     */
    public static PgPool getPgPoolByConnectionUrl(){
        String connectionUrl="postgresql://postgres:postgres@192.168.1.14:5432/xiongjie";
        PgPool client = PgClient.pool(connectionUrl);
        return client;
    }

    /**
     * 3.获取PgPool,通过Vert.x对象
     * @return
     */
    public static PgPool getPgPoolByVertx(){
        PgPoolOptions options = new PgPoolOptions()
                .setPort(5432)
                .setHost("192.168.1.14")
                .setDatabase("xiongjie")
                .setUser("postgres")
                .setPassword("postgres")
                .setMaxSize(5);
        Vertx vertx=Vertx.vertx();
        PgPool client = PgClient.pool(vertx, options);
        return client;
    }

    /**
     * 4.普通方法使用pgclient
     */
    public static void pgClient(){
        System.out.println("调用方法：pgClient");

        PgPool client = getPgPoolByOption();
//        PgPool client = getPgPoolByConnectionUrl();

        client.query("SELECT * FROM person WHERE name='xiongjie'", ar -> {
            if (ar.succeeded()) {
                PgRowSet result = ar.result();
                result.forEach(item -> {
                    System.out.println( item.getInteger("id") );
                    System.out.println( item.getString("name") );
                    System.out.println( item.getString("email") );
                });
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
            client.close();
        });
    }

    /**
     * 5.使用pgclient+vert.x
     * vert.x不会随着主程序结束而结束
     */
    public static void pgClientVertx(){
        System.out.println("调用方法：pgClientVertx");

        PgPool client = getPgPoolByVertx();

        client.getConnection(ar1 -> {
            if (ar1.succeeded()) {
                PgConnection conn = ar1.result();
                conn.query("SELECT * FROM person WHERE name='hexingjie'", ar2 -> {
                    if (ar2.succeeded()) {
                        PgRowSet result = ar2.result();
                        result.forEach(item -> {
                            System.out.println( item.getInteger("id") );
                            System.out.println( item.getString("name") );
                            System.out.println( item.getString("email") );
                        });
                        conn.close();
                    } else {
                        conn.close();
                    }
                    client.close();
                });
            } else {
                System.out.println("Could not connect: " + ar1.cause().getMessage());
                client.close();
            }
        });
    }

    /**
     * 6.使用pgclient,查询参数的封装
     * 主要方法client.preparedQuery()
     */
    public static void pgClientByFeature(){
        System.out.println("调用方法：pgClientByFeature");

        PgPool client = getPgPoolByOption();

        //参数化查询
        client.preparedQuery("SELECT * FROM person WHERE name=$1", Tuple.of("yuanzun"), ar -> {
            if (ar.succeeded()) {
                PgRowSet result = ar.result();
                result.forEach(item -> {
                    System.out.println( item.getInteger("id") );
                    System.out.println( item.getString("name") );
                    System.out.println( item.getString("email") );
                });
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
        });

        //批量执行，一般是插入/修改/删除操作,查询操作只会查询第一个存入的参数
        List<Tuple> batch = new ArrayList<>();
        batch.add(Tuple.of(5,"doupocangqiong"));
        batch.add(Tuple.of(6,"wudongqiankun"));


        client.preparedBatch("update  person set name=$2 where id=$1", batch, ar -> {
            if (ar.succeeded()) {
                System.out.println("数据修改成功！");
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
            }
            client.close();
        });
    }
}
